package com.sjp.myspring.context.support;

import com.sjp.myspring.beans.factory.JPBeanFactory;
import com.sjp.myspring.beans.factory.config.JPBeanDefinition;
import com.sjp.myspring.beans.factory.support.JPBeanDefinitionReader;
import com.sjp.myspring.beans.factory.support.JPDefaultListableBeanFactory;

import java.util.List;
import java.util.Properties;

/**
 * IOC DI MVC AOP
 */
public class JPWebApplicationContext extends JPAbstractApplicationContext {

    private String[] configLocations;
    private final JPDefaultListableBeanFactory beanFactory;
    private JPBeanDefinitionReader reader;

    public JPWebApplicationContext(String... configLocations) {
        this.configLocations = configLocations;
        this.beanFactory = new JPDefaultListableBeanFactory();
        this.reader = new JPBeanDefinitionReader(configLocations);
        this.refresh();
    }

    @Override
    public void refresh() {
        //1、定位配置文件

        //2、加载配置文件，扫描相关的类，把他们封装成beanDefinition
        List<JPBeanDefinition> beanDefinitions = reader.loadBeanDefinitions();

        //3、注册，把配置信息放到容器（伪IOC容器）中
        beanFactory.doRegisterBeanDefinition(beanDefinitions);

        //4、不是延时加载的类，提前初始化
        beanFactory.doAutowire();
    }

    @Override
    public Properties getConfig() {
        return reader.getConfig();
    }

    @Override
    public JPBeanFactory getBeanFactory() {
        return beanFactory;
    }



}
